package io.tan.searchestest.controller;

import com.detabes.search.es.util.EsIndexUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据操做示例
 *
 * @author lxw
 * @version V1.0
 * @date 2021/5/12
 **/
@RestController
@RequestMapping("dataTest")
public class DataTestController {
    private EsIndexUtil esIndexUtil;

    private final JdbcTemplate jdbcTemplate;

    public DataTestController(EsIndexUtil esIndexUtil, JdbcTemplate jdbcTemplate) {
        this.esIndexUtil = esIndexUtil;
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 测试向案卷类型的索引中添加数据
     *
     * @param index 索引名称 data_aj/data_xm 等
     * @return boolean
     * @author lxw
     * @date 2021/5/13 15:53
     */
    @PostMapping("bulkPostAppointId")
    public boolean bulkPostAppointId(String index) {
        String sql = "select top(100)* from TFQSDYDJM";
        List<Map<String, Object>> l = new ArrayList<>();
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> fubiao = new ArrayList<>();
            // 查询附表信息
            // 1  土地房屋权属抵押登记抵押人附表      TFQSDYDYRT
            sql = "select * from TFQSDYDYRT where DAH = '" + map.get("DAH") + "'";
            List<Map<String, Object>> list1 = jdbcTemplate.queryForList(sql);
            list1.forEach(i -> {
                i.put("dataId", "TFQSDYDYRT_" + i.get("id"));
                i.put("tableId", "45");
                i.put("tableChName", "土地房屋权属抵押登记房地产权证附表");
                i.put("tableEnName", "TFQSDYDYRT");
                fubiao.add(i);
            });
            // 2 土地房屋权属抵押登记房地产权证附表  TFQSDYFDCQZT
            sql = "select * from TFQSDYFDCQZT   where DAH = '" + map.get("DAH") + "'";
            List<Map<String, Object>> list2 = jdbcTemplate.queryForList(sql);
            list2.forEach(i -> {
                i.put("dataId", "TFQSDYFDCQZT_" + i.get("id"));
                i.put("tableId", "46");
                i.put("tableChName", "土地房屋权属抵押登记房地产权证附表");
                i.put("tableEnName", "TFQSDYFDCQZT");
                fubiao.add(i);
            });
            map.put("esId", "TFQSDYDJM_" + map.get("id"));
            map.put("dataId", "TFQSDYDJM_" + map.get("id"));
            map.put("tableId", "44");
            map.put("tableChName", "土地房屋权属抵押登记案卷表");
            map.put("tableEnName", "TFQSDYDJM");
            map.put("fubiao", fubiao);
            l.add(map);
        }
        //提交到ES索引中
        return esIndexUtil.bulkPostAppointId(index, l);
    }

}
